package com.superman.service.impl;

import com.superman.dao.IBrandDao;
import com.superman.dao.ICarDao;
import com.superman.dao.ICategoryDao;
import com.superman.dao.IRecordDao;
import com.superman.dao.impl.BrandDaoImpl;
import com.superman.dao.impl.CarDaoImpl;
import com.superman.dao.impl.CategoryDaoImpl;
import com.superman.dao.impl.RecordDaoImpl;
import com.superman.entity.Brand;
import com.superman.entity.Car_Brand_Category;
import com.superman.entity.Category;
import com.superman.entity.Record_User_Car;
import com.superman.service.IQuery;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class QueryImpl implements IQuery {
    ICarDao carDao = new CarDaoImpl();
    IRecordDao recordDao = new RecordDaoImpl();
    IBrandDao brandDao = new BrandDaoImpl();
    ICategoryDao categoryDao = new CategoryDaoImpl();

    /**
     * 用户查看所有的已上架的汽车记录
     * @return
     */
    @Override
    public List<Car_Brand_Category> userQueryCar() {
        List<Car_Brand_Category> list = new ArrayList<>();
        try {
            list = carDao.queryCarAll();
            //筛选查询结果，用户只能看到已上架的车辆
            Iterator<Car_Brand_Category> iterator = list.iterator();
            while (iterator.hasNext()){
                if (iterator.next().getUseable() == 1){
                    iterator.remove();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 管理员查看所有汽车记录
     * @return
     */
    @Override
    public List<Car_Brand_Category> adminQueryCar() {
        List<Car_Brand_Category> list = new ArrayList<>();
        try {
            list = carDao.queryCarAll();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 用户查看自己的租车记录
     * @param user_id 用户id
     * @return
     */
    @Override
    public List<Record_User_Car> userQueryRecord(String user_id) {
        List<Record_User_Car> list = new ArrayList<>();
        try {
            list = recordDao.queryRecordByUserID(user_id);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 管理员查看所有的租车记录
     * @return
     */
    @Override
    public List<Record_User_Car> adminQueryRecord() {
        List<Record_User_Car> list = new ArrayList<>();
        try {
            list = recordDao.queryRecordAll();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 查看指定用户尚未归还的租车记录信息
     * @param user_id
     * @return
     */
    @Override
    public List<Record_User_Car> queryRecordByUserIdAndNoReturnInfo(String user_id) {
        List<Record_User_Car> list = new ArrayList<>();
        try {
            list = recordDao.queryRecordByUserIdAndNoReturnInfo(user_id);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 查看所有品牌信息
     * @return 品牌对象集合
     */
    @Override
    public List<Brand> queryBrand() {
        List<Brand> list = new ArrayList<>();
        try {
            list = brandDao.queryBrand();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 查询所有类型信息
     * @return 类型对象集合
     */
    @Override
    public List<Category> queryCategory() {
        List<Category> list = null;
        try {
            list = categoryDao.queryCategory();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }


}
